package com.moon;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.umeng.message.UmengRegistrar;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ModifyUserActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_user);
		
		((TextView) findViewById(R.id.center)).setText("修改密码");
		((Button) findViewById(R.id.right)).setText("修改");
		findViewById(R.id.left).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		findViewById(R.id.right).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {

				EditText edit=(EditText)findViewById(R.id.text1);  
		        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
		        imm.hideSoftInputFromWindow(edit.getWindowToken(),0);		
				
		        edit=(EditText)findViewById(R.id.text2);  
		        imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
		        imm.hideSoftInputFromWindow(edit.getWindowToken(),0);
		        
		        edit=(EditText)findViewById(R.id.text3);  
		        imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
		        imm.hideSoftInputFromWindow(edit.getWindowToken(),0);
		        
				sendHttpImgRequest();
			}
		});
		
		JSONObject member = JSON.parseObject(PreferencesUtils.getString(this, "member"));
		if(member != null) ((TextView)findViewById(R.id.text0)).setText(member.getString("username"));
	}
	
	private void sendHttpImgRequest() {
		String pwd0 = ((TextView)findViewById(R.id.text1)).getText().toString();
		final String pwd1 = ((TextView)findViewById(R.id.text2)).getText().toString();
		String pwd2 = ((TextView)findViewById(R.id.text3)).getText().toString();
		
		if(!pwd2.equals(pwd1))
		{
			ToastUtil.showToastWithAlertPic("两次密码不一致");
			return;
		}
		
		String old = PreferencesUtils.getString(this, "password");
		if(!old.equals(pwd0))
		{
			ToastUtil.showToastWithAlertPic("原始密码不正确");
			return;
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("statusCode").equals("0")) {
					if(pwd1.length() > 0) PreferencesUtils.putString(ModifyUserActivity.this, "password", pwd1);
					sendRequest();
				}
				else
				{
					ToastUtil.showToastWithAlertPic(ruser.getString("message"));
				}
			}
			
			@Override
			public void onFailure(Throwable ble) {
				super.onFailure(ble);
				ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
			}
		};

		RequestParams params = new RequestParams();
		params.put("id", PreferencesUtils.getString(this, "userId"));
		params.put("password", pwd1);
		MgqRestClient.post("update_profile", params, loginHandler);
	}
	
	private void sendRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);
				if (ruser.getString("statusCode").equals("0")) {
					JSONObject member = ruser.getJSONObject("member");
					
					PreferencesUtils.putString(ModifyUserActivity.this, "member", JSON.toJSONString(member));
					PreferencesUtils.putString(ModifyUserActivity.this, "classes", JSON.toJSONString(member.getJSONArray("classes")));
					PreferencesUtils.putString(ModifyUserActivity.this, "children", JSON.toJSONString(member.getJSONArray("children")));
					finish();
				}
				else 
				{
					ToastUtil.showToastWithAlertPic(ruser.getString("message"));
				}
			}
			
			@Override
			public void onFailure(Throwable ble) {
				super.onFailure(ble);
				ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
			}
		};
		
		RequestParams params = new RequestParams();
		params.put("phonenumber", PreferencesUtils.getString(this, "username"));
		params.put("password", PreferencesUtils.getString(this, "password"));
		params.put("android_token", UmengRegistrar.getRegistrationId(this));//ios_token
		MgqRestClient.get("member_login", params, loginHandler);
	}
}
